package com.jhmk.jhecis.config;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import javax.sql.rowset.serial.SerialClob;
import java.sql.*;

@MappedJdbcTypes(JdbcType.CLOB)
@MappedTypes(Clob.class)
public class ClobTypeHandler extends BaseTypeHandler<String> {

    @Override
    public String getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return null;
    }

    @Override
    public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return null;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        //将String类型的参数转换成Clob类型的参数并设置给PreparedStatement对象
        Clob clob = new SerialClob(parameter.toCharArray());
        ps.setClob(i, clob);
    }
    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
    //从ResultSet对象的指定列中获取Clob类型的值，并将其转换成字符串类型的值返回
        Clob clob = rs.getClob(columnName);
        if (clob != null) {
            int len = (int) clob.length();
            return clob.getSubString(1, len);
        }
        return null;
    }
}
